package com.huajx.hanvon;

import Com.FirstSolver.Splash.FaceIdProtocolCodecFactory;
import com.huajx.core.Hboot;
import com.huajx.core.handler.HandlerAdapter;
import com.huajx.core.handler.ServerHandlerFactory;
import com.huajx.core.server.annonation.MinaServer;
import com.huajx.core.server.impl.BaseTcpServer;
import com.huajx.hanvon.config.HanvonConfig;
import com.huajx.hanvon.handler.*;
import com.huajx.util.CharsetUtil;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@MinaServer(name = "hanvon", port = 9230)
public class HanvonTcpServer extends BaseTcpServer {
    private static final Logger logger = LoggerFactory.getLogger(HanvonTcpServer.class);

    @Override
    protected ServerHandlerFactory getHandler() {
        return new ServerHandlerFactory() {
            @Override
            public HandlerAdapter getHandler(Object message) {
                if (message == null) {
                    logger.error("消息为空");
                    return null;
                }
                logger.info("{}", message);
                String msg = message.toString();
                if (msg.startsWith("PostRecord")) {
                    return new PostRecordHandler();
                } else if (msg.startsWith("Record")) {
                    return new RecordHandler();
                } else if (msg.startsWith("PostEmployee")) {
                    return new PostEmployeeHandler();
                } else if (msg.startsWith("Employee")) {
                    //return new EmployeeHandler();
                } else if (msg.startsWith("GetRequest")) {
                    return new GetRequestHandler();
                }else if (msg.startsWith("Return")) {
                	if(msg.contains("face_data=\"")){
                		 return new EmployeeHandler();
                	}else{
                		 return new ReturnHandler();
                	}
                } else if (msg.startsWith("Quit")) {
                    return new QuitHandler();
                }
                return null;
            }
        };
    }

    @Override
    protected ProtocolCodecFactory getProtocolCodecFactory() {
        return new FaceIdProtocolCodecFactory(CharsetUtil.CHARSET_GBK, true, false);
    }

    public static HanvonConfig config;

    @Override
    protected void initial() {
        config = Hboot.config(HanvonConfig.class);
    }
}
